package org.me.marble;

import java.nio.*;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;

/**
*
* @author tungi
*/

public class TextureFactory {
    private static int texture_id=1;
    
    public TextureFactory(){
    }

    public static ByteBuffer makeBufferFromBitmap(Bitmap bmp,int width,int height,int paddingLeft,int paddingTop){
    	//create an array which store pixels
        int[] pixels=new int[width*height];
        //get the config of the bitmap
        Config conf=bmp.getConfig();
        //create argb booleans
        boolean argb_8888=conf==Config.ARGB_8888?true:false;
        boolean argb_4444=conf==Config.ARGB_4444?true:false;
        boolean rgb_565=conf==Config.RGB_565?true:false;
        
        //if config is null, the bitmap is not in public format, 
        //but we need it during a game so we try to classify
        if(conf==null){
        	android.util.Log.println(android.util.Log.WARN,"WARNING",
				"Cannot recognize bitmap, maybe it will appear badly.");
        	if(bmp.hasAlpha()){
        		android.util.Log.i("I","Bitmap classified to ARGB8888 format.");
	        	argb_8888=true;
	        }
	        else{
	        	android.util.Log.i("I","Bitmap classified to RGB565 format.");
	        	rgb_565=true;
	        }
	    }
        
        int index=0;
        
        for(int y=height-1;y>=0;y--)
            for(int x=width-1;x>=0;x--){
                int pix=bmp.getPixel(width+paddingLeft-x-1,y+paddingTop);
                //convert argb to rgba depends on the config
                if(argb_8888 || rgb_565){
                    if(pix<0 && argb_8888)
                        pix=(int)Math.pow(16,8)+pix;
                    else if(pix<0 && rgb_565)
                        pix=(int)Math.pow(16,6)+pix;
                    
                    int alpha=0xff;
                    if(argb_8888)
                        alpha=pix >> 24;

                    
                    int rgb=(pix & 0x00ffffff) << 8;
                    pixels[index++]=(rgb | alpha);
                }
                else if(argb_4444){
                	if(pix<0)
                		pix=(int)Math.pow(2,16)+pix;
                	
                    int alpha=(pix >> 12) * 0x11;
                    int red=((pix >> 8) & 0xf) * 0x11;
                    int green=((pix >> 4) & 0xf) * 0x11;
                    int blue=(pix & 0xf) * 0x11;
                    pixels[index++]=((red << 24) | (green << 16) | (blue << 8) | alpha);
                }                    
//                else if(rgb_565){
//                    int alpha=0xff;
//                    int red=pix >> 11;
//                    int green=(pix >> 5) & 63;
//                    int blue=pix & 31;
//                    pixels[index++]=((red << 24) | (green << 16) | (blue << 8) | alpha);
//                }
            }
        
        return BufferFactory.MakeByteBuffer(pixels);
    }

    public static Texture loadTexture(int name,int type){
        Bitmap bmp=BitmapFactory.decodeResource(GameActivity.game.getResources(),name);
        return loadTexture(bmp,type);
    }

    public static Texture loadTexture(Bitmap bmp,int type){
//        int[] tmp=new int[1];
//        gl.glGenTextures(1,tmp,0);
        ByteBuffer _bb=makeBufferFromBitmap(bmp,bmp.getWidth(),bmp.getHeight(),0,0);
        return new Texture(texture_id++,type,bmp.getWidth(),bmp.getHeight(),_bb);
    }
    
    public static Texture loadTexture(Bitmap bmp,
    		int width,int height,int paddingLeft,int paddingTop,int type){
//      int[] tmp=new int[1];
//      gl.glGenTextures(1,tmp,0);
      ByteBuffer _bb=makeBufferFromBitmap(bmp,width,height,paddingLeft,paddingTop);
      return new Texture(texture_id++,type,width,height,_bb);
  }
}
